<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_pimage} function plugin
 *
 * Type:     function<br>
 * Name:     pp_pimage<br>
 * Date:     Feb 24, 2003<br>
 * Purpose:  format HTML tags for the image<br>
 * Input:<br>
 *         - file = file (and path) of image (required)
 *         - height = image height (optional, default actual height)
 *         - width = image width (optional, default actual width)
 *         - basedir = base directory for absolute paths, default
 *                     is environment variable DOCUMENT_ROOT
 *         - path_prefix = prefix for path output (optional, default empty)
 *
 * Examples: {pp_pimage file="/images/masthead.gif"}
 * Output:   <img src="/images/masthead.gif" width=400 height=23>
 * @link http://smarty.php.net/manual/en/language.function.html.image.php {pp_pimage}
 *      (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @author credits to Duda <duda@big.hu> - wrote first image function
 *           in repository, helped with lots of functionality
 * @version  1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_pimage($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
	
    $url = '';
    $src = '';
	$basedir = '';
	$path_prefix = '';
    $alt = '';
    $width = '';
    $height = '';
	$lw = '';
	$lh = '';
	$size = '';
	$pid = $id = '';
    $extra = '';
	
	$type = 'thumb';
	$product = NULL;
	
    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'url':
            case 'src':
			case 'basedir':
			case 'path_prefix':
            case 'alt':
            case 'width':
            case 'height':
            case 'lw':
			case 'lh':
			case 'size':
			case 'pid':
			case 'id':
                $$_key = $_val;
                break;
			
			case 'type':
			case 'product':
				$$_key = $_val;
				break;
			
            case 'link':
            case 'href':
                $prefix = '<a href="' . $_val . '">';
                $suffix = '</a>';
                break;
				
			default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("pp_pimage: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
				break;
		}
	}
	
	//
	if( substr($path_prefix, 0, 3) == '===' ) {
		$path_prefix = constant(substr($path_prefix, 3));
	}
	
	//
	if($src != '' && $size == 1 && (!isset($params['width']) || !isset($params['height']))) {
		if( substr($basedir, 0, 3) == '===' ) {
			$basedir = constant(substr($basedir, 3));
		}
		$_image_path = $basedir.$path_prefix.$src;
        if(!$_image_data = @getimagesize($_image_path)) {
            if(!file_exists($_image_path)) {
                $smarty->trigger_error("pp_pimage: unable to find '$_image_path'", E_USER_NOTICE);
                return;
            } else if(!is_readable($_image_path)) {
                $smarty->trigger_error("pp_pimage: unable to read '$_image_path'", E_USER_NOTICE);
                return;
            } else {
                $smarty->trigger_error("pp_pimage: '$_image_path' is not a valid image file", E_USER_NOTICE);
                return;
            }
        }
        if(!isset($params['width'])) {
            $width = $_image_data[0];
        }
        if(!isset($params['height'])) {
            $height = $_image_data[1];
        }
    }
	
	//
	if( isset($product) ) {
		$src = $product['products_image'];
		$width = $product['products_image_width'];
		$height = $product['products_image_height'];
		
		if( $type == 'thumb' ) {
			if( $url == '' ) {
				$url = $smarty->get_vars_context('PRODUCTTHUMBIMAGE');
			}
			if( $lw == '' ) {
				$lw = 120;
			}
			if( $lh == '' ) {
				$lh = 120;
			}
		} elseif( $type == 'resize' ) {
			if( $url == '' ) {
				$url = $smarty->get_vars_context('PRODUCTRESIZEIMAGE');
			}
			if( $lw == '' ) {
				$lw = 120;
			}
			if( $lh == '' ) {
				$lh = 120;
			}
		} elseif( $type == 'origin' ) {
			if( $url == '' ) {
				$url = $smarty->get_vars_context('PRODUCTORIGINIMAGE');
			}
			if( $lw == '' ) {
				$lw = 120;
			}
			if( $lh == '' ) {
				$lh = 120;
			}
		} else {
			if( $url == '' ) {
				$url = $smarty->get_vars_context('PRODUCTTHUMBIMAGE');
			}
			if( $lw == '' ) {
				$lw = 120;
			}
			if( $lh == '' ) {
				$lh = 120;
			}
		}
	}
	
	//
	if( $src == '' ) {
		return '';
	}
	
	if( $lw == '' && $lh == '' ) {
		if( strpos($url, 'thumb') !== false ) {
			$lw = 200;
			$lh = 200;
		} elseif( strpos($url, 'resize') !== false ) {
			$lw = 500;
			$lh = 500;
		}
	}
	
	if( $height > 0 && $width > 0 && $lw > 0 && $lh > 0 ) {
		$ratio = $width / $height;
		$ratio_set = $lw / $lh;
		if( $ratio >= $ratio_set ) {
			$lh = ceil($height * ( $lw / $width ));
		} else {
			$lw = ceil($width * ( $lh / $height ));
		}
	}
	
	if( $id != '' ) {
		$id = ' id="' . $pid . $id . '"';
	}
	
	//swf
	if( substr($src, -4) == '.swf' ) {
		if( $lw > 0 && $lh > 0 ) {
			$width = $lw;
			$height = $lh;
		}
		return $prefix . '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'.$width.'" height="'.$height.'" id="'.$id.'" align="middle">
<param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="'.$url.$path_prefix.$src.'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />
<embed src="'.$url.$path_prefix.$src.'" quality="high" bgcolor="#ffffff" width="'.$width.'" height="'.$height.'" name="'.$id.'" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
</object>'. $suffix;
	}
	
	//image
	if( $lw > 0 && $lh > 0 ) {
		return $prefix . '<img src="'.$url.$path_prefix.$src.'" alt="'.$alt.'" width="'.$lw.'" height="'.$lh.'"'.$id.$extra . '> '. $suffix;
	} elseif( $width > 0 && $height > 0 ) {
		return $prefix . '<img src="'.$url.$path_prefix.$src.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$id.$extra . '> '. $suffix;
	} else {
		return $prefix . '<img src="'.$url.$path_prefix.$src.'" alt="'.$alt.'" '.$id.$extra . '> '. $suffix;
	}
}
//
?>
